/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import DAO.DAOEmpresa;
import DAO.DAOFactory;
import DAO.DAOPaciente;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.LinkedList;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import modelo.Empresa;
import modelo.Paciente;

/**
 *
 * @author JGS
 */
public class Buscar_Paciente extends javax.swing.JInternalFrame {

    private static Buscar_Paciente instancia = null;

    public static Buscar_Paciente getInstancia() {
        if (instancia == null) {
            instancia = new Buscar_Paciente();
        } else {
            instancia.dispose();
            instancia = new Buscar_Paciente();
        }
        return instancia;
    }
    private SingletonPacientes pacientes;

    private Buscar_Paciente() {
        super("Buscar Paciente",
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        initComponents();
        groupButton();
        cargarEmpresas();
        pacientes = SingletonPacientes.getInstancia();
        documento.setEnabled(false);
        empresa.setEnabled(false);
        apellido.setEnabled(true);
    }

    private void cargarEmpresas() {
        DAOEmpresa dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getEmpresaDAO();
        LinkedList<Empresa> lista = (LinkedList) dao.list();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < lista.size(); i++) {
            Empresa aux = lista.get(i);
            model.addElement(aux);
        }
        empresa.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        radioDocumento = new javax.swing.JRadioButton();
        jButton5 = new javax.swing.JButton();
        buscar = new javax.swing.JButton();
        empresa = new javax.swing.JComboBox();
        apellido = new javax.swing.JTextField();
        documento = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPacientes = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        radioEmpresa = new javax.swing.JRadioButton();
        jButton3 = new javax.swing.JButton();
        radioApellido = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        radioDocumento.setText("Por Documento");
        radioDocumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioDocumentoActionPerformed(evt);
            }
        });

        jButton5.setLabel("Cerrar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        buscar.setText("Buscar");
        buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarActionPerformed(evt);
            }
        });

        empresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tablaPacientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tablaPacientes);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel3.setForeground(new java.awt.Color(153, 153, 153));
        jLabel3.setText("Pacientes");

        radioEmpresa.setText("Por Empresa");
        radioEmpresa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioEmpresaActionPerformed(evt);
            }
        });

        jButton3.setText("Nuevo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        radioApellido.setSelected(true);
        radioApellido.setText("Por Apellido");
        radioApellido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioApellidoActionPerformed(evt);
            }
        });

        jButton1.setText("Ver..");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(10, 10, 10))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(580, 580, 580)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(40, 40, 40)
                                .addComponent(jButton1)
                                .addGap(31, 31, 31)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 871, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(19, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioApellido)
                            .addComponent(radioEmpresa)
                            .addComponent(radioDocumento))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(documento, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                            .addComponent(empresa, 0, 235, Short.MAX_VALUE)
                            .addComponent(apellido, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))
                        .addGap(271, 271, 271)
                        .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(191, 191, 191))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton3, jButton5});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioApellido)
                    .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(empresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioEmpresa))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(documento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radioDocumento))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(11, 11, 11))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton3, jButton5});

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    this.dispose();
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    Principal princ = Principal.getInstancia();
    Alta_Paciente alta = Alta_Paciente.getInstancia();
    princ.desktop.add(alta);
    alta.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension ventana = alta.getSize();
    alta.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);
    alta.setVisible(true);
    this.dispose();
}//GEN-LAST:event_jButton3ActionPerformed

private void buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarActionPerformed
    if (radioApellido.isSelected()) {
        if (apellido.getText().length() == 0) JOptionPane.showMessageDialog(this,
                "Ingrese un apellido");
        else buscarPorApellido();
    }
    if (radioEmpresa.isSelected()) {
        if (empresa.getSelectedIndex() == -1) JOptionPane.showMessageDialog(this,
                "seleccione una empresa");
        else buscarPorEmpresa();
    }
    if (radioDocumento.isSelected()) {
        if (documento.getText().length() == 0) JOptionPane.showMessageDialog(this,
                "Ingrese un documento");
        else buscarPorDocumento();
    }
}//GEN-LAST:event_buscarActionPerformed

private void radioApellidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioApellidoActionPerformed
    apellido.setEnabled(true);
    empresa.setEnabled(false);
    documento.setEnabled(false);
}//GEN-LAST:event_radioApellidoActionPerformed

private void radioEmpresaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioEmpresaActionPerformed
    apellido.setEnabled(false);
    empresa.setEnabled(true);
    documento.setEnabled(false);
}//GEN-LAST:event_radioEmpresaActionPerformed

private void radioDocumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioDocumentoActionPerformed
    apellido.setEnabled(false);
    empresa.setEnabled(false);
    documento.setEnabled(true);
}//GEN-LAST:event_radioDocumentoActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
     Principal princ = Principal.getInstancia();
    verPaciente alta = new verPaciente();
    princ.desktop.add(alta);
    alta.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension ventana = alta.getSize();
    alta.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);
    alta.setVisible(true);
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

    private void buscarPorEmpresa() {
        DAOPaciente dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
        pacientes.setPacientes((LinkedList) dao.buscarPorEmpresa((Empresa) empresa.getSelectedItem()));
        ModeloTabla modelo = new ModeloTabla();
        Object tit[] = {"LEGAJO", "APELLIDO", "NOMBRE", "NRO DOCUMENTO", "FECHA NACIMIENTO", "EMPRESA"};
        Object vals[][] = new Object[pacientes.getPacientes().size()][tit.length];
        for (int i = 0; i < pacientes.getPacientes().size(); i++) {
            Paciente aux = pacientes.getPacientes().get(i);
            Object datos[] = new Object[tit.length];
            datos[0] = aux.getNumeroLegajo();
            datos[1] = aux.getApellido();
            datos[2] = aux.getNombre();
            datos[3] = aux.getNumeroDoc();
            datos[4] = aux.getFechaNacimiento();
            datos[5] = aux.getEmpresa().getRazonSocial();
            vals[i] = datos;
        }
        modelo.setDataVector(vals, tit);
        this.tablaPacientes.setModel(modelo);
        if (pacientes.getPacientes().size() == 0) {
            JOptionPane.showMessageDialog(this, "No se han encontrado pacientes");
        }
    }

    private void buscarPorDocumento() {
        DAOPaciente dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
        pacientes.setPacientes((LinkedList) dao.buscarPorDocumento(Integer.parseInt(documento.getText())));
        ModeloTabla modelo = new ModeloTabla();
        Object tit[] = {"LEGAJO", "APELLIDO", "NOMBRE", "NRO DOCUMENTO", "FECHA NACIMIENTO", "EMPRESA"};
        Object vals[][] = new Object[pacientes.getPacientes().size()][tit.length];
        for (int i = 0; i < pacientes.getPacientes().size(); i++) {
            Paciente aux = pacientes.getPacientes().get(i);
            Object datos[] = new Object[tit.length];
            datos[0] = aux.getNumeroLegajo();
            datos[1] = aux.getApellido();
            datos[2] = aux.getNombre();
            datos[3] = aux.getNumeroDoc();
            datos[4] = aux.getFechaNacimiento();
            datos[5] = aux.getEmpresa().getRazonSocial();
            vals[i] = datos;
        }
        modelo.setDataVector(vals, tit);
        this.tablaPacientes.setModel(modelo);
        if (pacientes.getPacientes().size() == 0) {
            JOptionPane.showMessageDialog(this, "No se han encontrado pacientes");
        }
    }

    private void buscarPorApellido() {

        DAOPaciente dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
        pacientes.setPacientes((LinkedList) dao.buscarPorApellido(apellido.getText()));
        ModeloTabla modelo = new ModeloTabla();
        Object tit[] = {"LEGAJO", "APELLIDO", "NOMBRE", "NRO DOCUMENTO", "FECHA NACIMIENTO", "EMPRESA"};
        Object vals[][] = new Object[pacientes.getPacientes().size()][tit.length];
        for (int i = 0; i < pacientes.getPacientes().size(); i++) {
            Paciente aux = pacientes.getPacientes().get(i);
            Object datos[] = new Object[tit.length];
            datos[0] = aux.getNumeroLegajo();
            datos[1] = aux.getApellido();
            datos[2] = aux.getNombre();
            datos[3] = aux.getNumeroDoc();
            datos[4] = aux.getFechaNacimiento();
            datos[5] = aux.getEmpresa().getRazonSocial();
            vals[i] = datos;
        }
        modelo.setDataVector(vals, tit);
        this.tablaPacientes.setModel(modelo);
        if (pacientes.getPacientes().size() == 0) {
            JOptionPane.showMessageDialog(this, "No se han encontrado pacientes");
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField apellido;
    private javax.swing.JButton buscar;
    private javax.swing.JFormattedTextField documento;
    private javax.swing.JComboBox empresa;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JRadioButton radioApellido;
    private javax.swing.JRadioButton radioDocumento;
    private javax.swing.JRadioButton radioEmpresa;
    private javax.swing.JTable tablaPacientes;
    // End of variables declaration//GEN-END:variables

    private void groupButton() {
        ButtonGroup busca = new ButtonGroup();
        busca.add(radioApellido);
        busca.add(radioEmpresa);
        busca.add(radioDocumento);
    }
}

